package com.GZX;

import com.WKH.InfoManage;
import com.publicClass.CRUD;
import com.publicClass.Role;
import com.publicClass.User;
import com.util.Vessel.*;

import java.util.ArrayList;
import java.util.List;

import static com.WKH.InfoManage.*;
import static com.util.Vessel.scanner;
import static com.util.Vessel.*;


public class Account {

    public static CRUD<User> userCRUD = new CRUD<>();

    //账号注册
    public static List<User> signUp(){
        id++;
        System.out.println("请输入用户名称：");
        String inName = scanner.next();
        System.out.println("请设置密码：");
        String inPassword = scanner.next();
        System.out.println("请设置性别：");
        String inSex = scanner.next();
        System.out.println("请输入年龄：");
        int inAge = scanner.nextInt();
        System.out.println("请输入联系方式：");
        String inContract = scanner.next();
        System.out.println("请选择身份：1：学生   2：教师   3：管理员");
        int inChoice = scanner.nextInt();
        Role inrole = null;

        switch (inChoice){
            case 1:
                inrole = Role.Student;
                break;
            case 2:
                inrole = Role.Teacher;
                break;
            case 3:
                inrole = Role.Manager;
                break;
        }
        if (inrole == Role.Student){
            System.out.println("请输入专业： ");
            String inMajor = scanner.next();
            System.out.println("请输入班级名称：");
            String inClass = scanner.next();
            System.out.println("请输入宿舍号：");
            int inDid = scanner.nextInt();
            System.out.println("请输入年级：");
            String inGrade = scanner.next();
            User newUser = new User(id,inName,inPassword,inSex,inAge,inContract,inMajor,inrole,inClass,inDid,inGrade);
            userCRUD.add(newUser,users);
            System.out.println("账号注册成功如下是您的账号信息：");
            System.out.println("账号\t\t\t\t\t\t" + "姓名\t\t\t" + "密码\t\t\t" + "性别\t\t\t" + "年龄\t\t\t" + "联系方式\t\t\t" + "专业\t\t\t" + "身份角色\t\t\t" + "所在班级\t\t\t" + "宿舍编号\t\t\t" + "年级\t\t\t");
            System.out.println(newUser.toStringL1());
        }else if (inrole == Role.Teacher){
            System.out.println("请输入专业： ");
            String inMajor = scanner.next();
            User newUser = new User(id,inName,inPassword,inSex,inAge,inContract,inMajor,inrole);
            tmUser.add(newUser);
            System.out.println("账号注册申请成功如下是您申请的账号信息：");
            System.out.println("账号\t\t\t" + "姓名\t\t\t" + "密码\t\t\t" + "性别\t\t\t" + "年龄\t\t\t" + "联系方式\t\t\t" + "教授专业\t\t\t" + "身份角色\t\t\t");
            System.out.println(newUser.toString());
        }else if (inrole == Role.Manager){
            User newUser = new User(id,inName,inPassword,inContract,inrole);
            tmUser.add(newUser);
            System.out.println("账号\t\t\t" + "姓名\t\t\t" + "密码\t\t\t" + "联系方式\t\t\t" + "身份角色\t\t\t");
            System.out.println(newUser.toStringL3());
        }
        return tmUser;
    }


    //教师管理员账号审核
    public static void check(List<User> tmUser){
        System.out.println("申请人账号" + "\t\t\t" +"申请人姓名" + "\t\t\t" + "申请人设置密码" + "\t\t\t" + "申请人联系方式：" + "\t\t\t" + "申请身份权限");
        for (User user : tmUser) {
            if (user.getRole() == Role.Manager || user.getRole() == Role.Teacher){
                System.out.println(user.getId() + "\t\t\t" +user.getName() + "\t\t\t" + user.getPassword()+ "\t\t\t" + user.getContract() + "\t\t\t" + user.getRole());
            }
        }
        System.out.println("是否批准：1：全批准  2：部分驳回批准剩余  3：全部驳回！");
        int choice = scanner.nextInt();
        switch (choice){
            case 1:
                allApprove(tmUser);
                break;
            case 2:
                reject(tmUser);
                break;
            case 3:
                tmUser.clear();
                break;
        }
    }

    //全部批准
    public static String allApprove(List<User> tmUser){
        for (User user : tmUser){
            if (user.getRole() == Role.Manager || user.getRole() == Role.Teacher){
                userCRUD.add(user,users);
            }
        }
        return "已全部批准同意！";
    }

    //部分驳回，剩余全批准
    public static String reject(List<User> tmUser){
        boolean isFlag = true;
        do {
            System.out.println("请输入驳回账号：");
            int rejectId = scanner.nextInt();
            for (User user : tmUser){
                if (user.getId() == rejectId){
                    tmUser.remove(user);
                    break;
                }
            }
            System.out.println("是否继续驳回：1：是  2：将剩余账号批准注册！");
            int isflag = scanner.nextInt();
            isFlag = (isflag == 1);
        }while(isFlag);
        allApprove(tmUser);
        return "已将剩余账号全部批准同意！";
    }

    //登录账号
    public static void signIn(){
        boolean isNext = false;
        int count = 0;
        do {
            isNext = false;
            System.out.println("请输入账号： ");
            long inputId = scanner.nextLong();
            System.out.println("请输入密码： ");
            String inputPassword = scanner.next();
            boolean isFound = false;
            for (User user : users){
                if (user.getId() == inputId && user.getPassword().equals(inputPassword)){
                    verify(user);
                    isFound = true;
                    break;
                }
            }
            if (!isFound && count < 2){
                System.out.println("账号不存在或密码错误！！！");
                count++;
                System.out.println("请选择：1：请重新尝试   2：进入登录注册页面");
                int input = scanner.nextInt();
                isNext = (input == 1);
            } else if (count > 3) {
                System.out.println("操作失误过多，请确认账号是否存在，或通过线下请求管理员更改密码！！");
                isNext = false;
            }
        }while(isNext);
    }

    //身份验证
    public static void verify(User user){
        long userId = 0;
        if (user.getRole() == Role.Manager){
            adminOperation(user.getId());
        } else if (user.getRole() == Role.Teacher) {
            teaOperation(user.getId());
        } else if (user.getRole() == Role.Student) {
            stuOperation(user.getId());

        }
    }

}
